package GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import database.TableManager;
import item.*;

public class GUI_page02_ProcessPurchase extends JPanel {
	TableManager tm;
	List<ShoppingCartItem> shoppingList;
	
	JPanel paneLeft = new JPanel();
	JPanel paneRight = new JPanel();
	JPanel paneLeftBottom = new JPanel();
	
	JLabel titleLabel;
	JLabel titleLabelLeft;
	JLabel titleLabelRight;
	JButton returnButton;
	
	JLabel nameLabel;
	List<String> storeOptions;
	JComboBox storeList;
	
	JLabel dateLabel;
	JTextField dateField;
	JPanel paymentOptionsPane = new JPanel(); // horizontal pane for payment type stuff
	JPanel paymentTypePane = new JPanel();
	JLabel paymentTypeLabel;
	static String[] paymentTypes = { "Cash", "Credit Card" };
	JComboBox paymentTypeList;
	JPanel paymentCCNPane = new JPanel();
	JLabel creditCardNumberLabel;
	JTextField creditCardNumberField;
	JPanel paymentCCExpPane = new JPanel(); 
	JLabel creditCardExpiryLabel;
	JTextField creditCardExpiryField;
	
	JPanel UPCAndFoundItemPane = new JPanel();
	JLabel UPCLabel;
	JTextField UPCField;
	JLabel foundItemLabel;
	JLabel quantityLabel;
	JTextField quantityField;
	JButton addItemButton;
	JPanel listAndRemovePane = new JPanel();
	JList shoppingJList;
	JScrollPane shoppingJListScroller;
	DefaultListModel listModel;
	JButton removeItemButton;
	
	JButton processAndSaveButton;
	
	public GUI_page02_ProcessPurchase() {
		storeOptions = new ArrayList<String>();
		storeOptions.add("None");

		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.setBackground(Color.white);
		
		// for store stuff
		paneLeft.setLayout(new BoxLayout(paneLeft, BoxLayout.Y_AXIS));
		paneLeft.setAlignmentX(LEFT_ALIGNMENT);
		paneLeft.setMaximumSize(new Dimension(400, 450));
		paneLeft.setBackground(Color.white);
		this.add(paneLeft);
		
		this.add(Box.createRigidArea(new Dimension(20,0)));
		
		// for shopping item stuff
		paneRight.setLayout(new BoxLayout(paneRight, BoxLayout.Y_AXIS));
		paneRight.setAlignmentX(LEFT_ALIGNMENT);
		paneRight.setMaximumSize(new Dimension(400, 450));
		paneRight.setBackground(Color.WHITE);
		this.add(paneRight);
		
		// LEFT PANE STUFF
		
		// title label
		titleLabel = new JLabel("Clerk - Process Purchase", JLabel.CENTER);
		titleLabel.setVerticalTextPosition(JLabel.CENTER);
		
		titleLabel.setHorizontalTextPosition(JLabel.CENTER);
		titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		titleLabel.setFont(new Font("Century Gothic", Font.BOLD, 20));
		titleLabel.setForeground(Color.gray);
		paneLeft.add(titleLabel);
        
		paneLeft.add(Box.createRigidArea(new Dimension(0,15)));
		
		 // return button
		returnButton = GUI_AMSStoreMain.makeGrayWhiteButton("Cancel", Component.LEFT_ALIGNMENT);
		paneLeft.add(returnButton);
        
		paneLeft.add(Box.createRigidArea(new Dimension(0,10)));
		
		// title label left
		titleLabelLeft = new JLabel("Store of Purchase", JLabel.CENTER);
		titleLabelLeft.setVerticalTextPosition(JLabel.CENTER);
		
		titleLabelLeft.setHorizontalTextPosition(JLabel.CENTER);
		titleLabelLeft.setAlignmentX(Component.LEFT_ALIGNMENT);
		titleLabelLeft.setFont(new Font("Century Gothic", Font.BOLD, 17));
		titleLabelLeft.setForeground(Color.gray);
		paneLeft.add(titleLabelLeft);
        
		paneLeft.add(Box.createRigidArea(new Dimension(0,10)));

        // store name field
        nameLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Store Name", 11);
        paneLeft.add(nameLabel);
        // list of stores to choose from, by default on first one
        Object[] tableStrings = storeOptions.toArray();
        storeList = new JComboBox(tableStrings);
        storeList.setSelectedIndex(0);
        storeList.setAlignmentX(Component.LEFT_ALIGNMENT);
        storeList.setForeground(Color.gray);
        storeList.setBackground(Color.white);
        storeList.setFont(new Font("Century Gothic", Font.PLAIN, 13));
        storeList.setMaximumSize(new Dimension(200,20));
        storeList.setSize(new Dimension(200, 20)); 
        paneLeft.add(storeList);
		
        paneLeft.add(Box.createRigidArea(new Dimension(0,5)));
		
		// date field
		dateLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Current Date (yyyy-mm-dd)", 11);
		paneLeft.add(dateLabel);
		dateField = GUI_AMSStoreMain.makeLeftGrayTextField("yyyy-mm-dd", 80);
		paneLeft.add(dateField);
		//page2ClerkPP_DateField.addActionListener(this);
		
		paneLeft.add(Box.createRigidArea(new Dimension(0,5)));
		
		//paneLeft.add(Box.createRigidArea(new Dimension(0,5)));
		
		// Payment Type panes/labels/fields
		paymentOptionsPane.setLayout(new BoxLayout(paymentOptionsPane, BoxLayout.X_AXIS)); // horizontal pane for payment stuff
		paymentOptionsPane.setAlignmentX(LEFT_ALIGNMENT);
		paymentOptionsPane.setMaximumSize(new Dimension(400, 40));
		paymentOptionsPane.setBackground(Color.white);
		paymentTypePane.setLayout(new BoxLayout(paymentTypePane, BoxLayout.Y_AXIS)); // mini vertical pane for payment type label/list
		paymentTypePane.setAlignmentX(LEFT_ALIGNMENT);
		paymentTypePane.setAlignmentY(TOP_ALIGNMENT);
		paymentTypePane.setMaximumSize(new Dimension(140, 40));
		paymentTypePane.setBackground(Color.white);
		paymentCCNPane.setLayout(new BoxLayout(paymentCCNPane, BoxLayout.Y_AXIS)); // mini vertical pane for CCN label/field
		paymentCCNPane.setAlignmentX(LEFT_ALIGNMENT);
		paymentCCNPane.setAlignmentY(TOP_ALIGNMENT);
		paymentCCNPane.setMaximumSize(new Dimension(130, 40));
		paymentCCNPane.setBackground(Color.white);
		paymentCCNPane.setVisible(false);
		paymentCCExpPane.setLayout(new BoxLayout(paymentCCExpPane, BoxLayout.Y_AXIS)); // mini vertical pane for CCN expire/field
		paymentCCExpPane.setAlignmentX(LEFT_ALIGNMENT);
		paymentCCExpPane.setAlignmentY(TOP_ALIGNMENT);
		paymentCCExpPane.setMaximumSize(new Dimension(130, 40));
		paymentCCExpPane.setBackground(Color.white);
		paymentCCExpPane.setVisible(false);
		// payment type dropdown
		paymentTypeLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Payment Type", 11);
		paymentTypePane.add(paymentTypeLabel);
		paymentTypeList = new JComboBox(paymentTypes);
		paymentTypeList.setSelectedIndex(0);
		paymentTypeList.setAlignmentX(Component.LEFT_ALIGNMENT);
		paymentTypeList.setForeground(Color.gray);
		paymentTypeList.setBackground(Color.white);
		paymentTypeList.setFont(new Font("Century Gothic", Font.PLAIN, 12));
		paymentTypeList.setMaximumSize(new Dimension(120,22));
		paymentTypePane.add(paymentTypeList);
		paymentOptionsPane.add(paymentTypePane);
		// payment Credit card number (initially hidden)
		creditCardNumberLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Credit Card #", 11);
		paymentCCNPane.add(creditCardNumberLabel);
		creditCardNumberField = GUI_AMSStoreMain.makeLeftGrayTextField("", 120);
		paymentCCNPane.add(creditCardNumberField);
		paymentOptionsPane.add(paymentCCNPane);
		// payment expiry date
		creditCardExpiryLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Expiry (yyyy-mm-dd)", 11);
		paymentCCExpPane.add(creditCardExpiryLabel);
		creditCardExpiryField = GUI_AMSStoreMain.makeLeftGrayTextField("yyyy-mm-dd", 80);
		paymentCCExpPane.add(creditCardExpiryField);
		paymentOptionsPane.add(paymentCCExpPane);
		
		paneLeft.add(paymentOptionsPane);

		paneLeft.add(Box.createRigidArea(new Dimension(55,100)));
		
		// area to push over process button:
		paneLeftBottom.setLayout(new BoxLayout(paneLeftBottom, BoxLayout.X_AXIS));
		paneLeftBottom.setAlignmentX(LEFT_ALIGNMENT);
		paneLeftBottom.setMaximumSize(new Dimension(400, 80));
		paneLeftBottom.setBackground(Color.WHITE);
		paneLeft.add(paneLeftBottom);
		
		paneLeftBottom.add(Box.createRigidArea(new Dimension(220,0)));

        // final process button
        processAndSaveButton = GUI_AMSStoreMain.makeGrayWhiteButton("Process & Save", Component.LEFT_ALIGNMENT);
        processAndSaveButton.setForeground(Color.RED);
        paneLeftBottom.add(processAndSaveButton);
		
		
		// RIGHT PANE STUFF
        
        paneRight.add(Box.createRigidArea(new Dimension(0,45)));
		
		// title label right
		titleLabelRight = new JLabel("Items Purchased", JLabel.CENTER);
		titleLabelRight.setVerticalTextPosition(JLabel.CENTER);
		
		titleLabelRight.setHorizontalTextPosition(JLabel.CENTER);
		titleLabelRight.setAlignmentX(Component.LEFT_ALIGNMENT);
		titleLabelRight.setFont(new Font("Century Gothic", Font.BOLD, 17));
		titleLabelRight.setForeground(Color.gray);
		paneRight.add(titleLabelRight);
        
		paneRight.add(Box.createRigidArea(new Dimension(0,10)));
		
		// UPC labels and field
		UPCLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Item UPC", 11);
		paneRight.add(UPCLabel);
		// Pane to hold UPC field and if the given UPC results in an item
		UPCAndFoundItemPane.setLayout(new BoxLayout(UPCAndFoundItemPane, BoxLayout.X_AXIS));
		UPCAndFoundItemPane.setAlignmentX(LEFT_ALIGNMENT);
		UPCAndFoundItemPane.setMaximumSize(new Dimension(400, 25));
		UPCAndFoundItemPane.setBackground(Color.WHITE);
		paneRight.add(UPCAndFoundItemPane);
		// UPC field and item-found label horizontally on ^ pane
		UPCField = GUI_AMSStoreMain.makeLeftGrayTextField("", 110);
		UPCAndFoundItemPane.add(UPCField);
		UPCAndFoundItemPane.add(Box.createRigidArea(new Dimension(5,0)));
		foundItemLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("", 11);
		UPCAndFoundItemPane.add(foundItemLabel);
		
		paneRight.add(Box.createRigidArea(new Dimension(0,2)));
		
		// Quantity field
		quantityLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Item Quantity", 11);
		paneRight.add(quantityLabel);
		quantityField = GUI_AMSStoreMain.makeLeftGrayTextField("1", 50);
		paneRight.add(quantityField);
		//page2ClerkPP_DateField.addActionListener(this);
		
		paneRight.add(Box.createRigidArea(new Dimension(0,10)));
		
		// add item to shopping cart button
		addItemButton = GUI_AMSStoreMain.makeGrayWhiteButton("Add Item", Component.LEFT_ALIGNMENT);
		paneRight.add(addItemButton);
        
		paneRight.add(Box.createRigidArea(new Dimension(0,15)));
		
		// list of shopping items (pane with remove button too)
		listAndRemovePane.setLayout(new BoxLayout(listAndRemovePane, BoxLayout.X_AXIS));
		listAndRemovePane.setAlignmentX(LEFT_ALIGNMENT);
		listAndRemovePane.setMaximumSize(new Dimension(400, 100));
		listAndRemovePane.setBackground(Color.WHITE);
		paneRight.add(listAndRemovePane);
		listModel = new DefaultListModel();
		listModel.addElement("Empty");
		shoppingJList = new JList(listModel);
		shoppingJList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		shoppingJList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		shoppingJList.setVisibleRowCount(-1);
		shoppingJList.setSelectedIndex(0);
		shoppingJListScroller = new JScrollPane(shoppingJList);
		shoppingJListScroller.setPreferredSize(new Dimension(250, 85));
		listAndRemovePane.add(shoppingJListScroller);
		listAndRemovePane.add(Box.createRigidArea(new Dimension(4,0)));
		removeItemButton = GUI_AMSStoreMain.makeGrayWhiteButton("Remove Selection", Component.LEFT_ALIGNMENT);
		listAndRemovePane.add(removeItemButton);
	}
	
	public void resetPage()
	{
		this.rePopulateStoreList();
		this.processAndSaveButton.setEnabled(false);
		this.addItemButton.setEnabled(false);
		this.removeItemButton.setEnabled(false);
		this.UPCField.setText("");
		this.quantityField.setText("1");
		
		shoppingList = new ArrayList<ShoppingCartItem>();
		shoppingJListScroller.remove(shoppingJList);
		listAndRemovePane.remove(shoppingJListScroller);
		
		listModel = new DefaultListModel();
		listModel.addElement("Empty");
		shoppingJList = new JList(listModel);
		shoppingJList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		shoppingJList.setLayoutOrientation(JList.VERTICAL);
		shoppingJList.setVisibleRowCount(-1);
		shoppingJList.setSelectedIndex(0);
		shoppingJListScroller = new JScrollPane(shoppingJList);
		shoppingJListScroller.setPreferredSize(new Dimension(250, 85));
		listAndRemovePane.add(shoppingJListScroller,0);
	}
	
	private void rePopulateStoreList()
	{
		paneLeft.remove(storeList);
		// list of stores to choose from, by default on first one
        Object[] tableStrings = storeOptions.toArray();
        storeList = new JComboBox(tableStrings);
        storeList.setSelectedIndex(0);
        storeList.setAlignmentX(Component.LEFT_ALIGNMENT);
        storeList.setForeground(Color.gray);
        storeList.setBackground(Color.white);
        storeList.setFont(new Font("Century Gothic", Font.PLAIN, 13));
        storeList.setMaximumSize(new Dimension(200,25));
        storeList.setSize(new Dimension(200, 25));
        paneLeft.add(storeList,7);
	}
}
